import random
from function_tool import time_format_transform
import uuid
import clickhouse_connect
import datetime

connect_info = {
    'port': 8123,
    'host': '192.168.104.243',
    'username': 'default',
    'password': 'bwda2025',
    'database': 'lap'
}
client = clickhouse_connect.get_client(**connect_info)
column_list = ['bwd_create_time', 'id', 'op_time', 'log_from', 'app_session_id',
               'master_account_name', 'person_name', 'master_org_id', 'master_org_name', 'master_status',
               'master_have_vpn',
               'master_account_type', 'slave_account_name', 'slave_account_type', 'slave_status', 'resource_code',
               'resource_name',
               'client_ip', 'service_ip', 'service_port', 'op_content', 'op_result', 'err_code', 'err_msg']
log_from_list = ['DWD', 'DWS', 'DWM', 'DWA', 'DWB', 'DWC', 'DWD_APP', 'DWD_APP_LOGS', 'DWD_APP_LOGS_LOCAL',
                 'DWD_APP_LOGS_REMOTE', 'DWD_APP_LOGS_SLAVE', 'DWD_APP_LOGS_SLAVE_LOCAL', 'DWD_APP_LOGS_SLAVE_REMOTE',
                 'DWD_APP_LOGS_SLAVE_REMOTE']
resource_code_list = ['bass_gp', 'service', 'ADG', 'CRM', 'bass', 'bdata', 'bass_mod', 'GBASE', 'bi', 'zevendb2',
                      'ngboss']
master_status_list = ['0', '1', '2', '3']
master_have_vpn_list = ['0', '1']
master_account_type_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
slave_account_type_list = ['0', '1', '2', '3', '4']
slave_status_list = ['0', '1', '2', '3']
op_result_list = ['0', '1']
insert_data = []
table = 'dwd_app_logs_local'
err_list = [{'err_code': '0', 'err_msg': ''}, {'err_code': '1', 'err_msg': '日志关键字段为空'},
            {'err_code': '2', 'err_msg': 'IP格式异常'},
            {'err_code': '3', 'err_msg': '时间格式异常'}, {'err_code': '4', 'err_msg': '日志关键字段缺失'},
            {'err_code': '0', 'err_msg': ''}, {'err_code': '0', 'err_msg': ''}, {'err_code': '0', 'err_msg': ''},
            {'err_code': '0', 'err_msg': ''}, {'err_code': '0', 'err_msg': ''}, {'err_code': '0', 'err_msg': ''},
            {'err_code': '0', 'err_msg': ''},
            {'err_code': '0', 'err_msg': ''},
            {'err_code': '0', 'err_msg': ''},
            {'err_code': '0', 'err_msg': ''}]
length = len(err_list)
for i in range(4000000):
    index = random.randint(0, length - 1)
    bwd_create_time = time_format_transform.TimeFormatTransform().get_random_date('2025-10-01', '2025-11-01')
    sid = str(random.randint(10 ** 14, 10 ** 16))
    minute = random.randint(0, 20)
    op_time = bwd_create_time - datetime.timedelta(seconds=random.randint(0, 60 * minute))
    log_from = random.choice(log_from_list)
    app_session_id = uuid.uuid4().hex
    master_account_name = 'luffy' + str(i).zfill(8)
    person_name = '路飞' + str(i).zfill(8)
    master_org_id = str(random.choice(range(10 ** 14, 10 ** 16)))
    master_org_name = '数字智能部'
    master_status = random.choice(master_status_list)
    master_have_vpn = random.choice(master_have_vpn_list)
    master_account_type = random.choice(master_account_type_list)
    slave_account_name = 'nika' + str(i).zfill(8)
    slave_account_type = random.choice(slave_account_type_list)
    slave_status = random.choice(slave_status_list)
    resource_code = random.choice(resource_code_list)
    resource_name = resource_code
    client_ip = '10.179.211.151'
    service_ip = '10.179.211.146'
    service_port = str(random.choice(range(1000, 10000)))
    op_content = 'select * from sys_dept'
    op_result = random.choice(op_result_list)
    err_code = err_list[index]['err_code']
    err_msg = err_list[index]['err_msg']
    insert_data.append([bwd_create_time, sid, op_time, log_from, app_session_id,
                        master_account_name, person_name, master_org_id, master_org_name, master_status,
                        master_have_vpn,
                        master_account_type, slave_account_name, slave_account_type, slave_status, resource_code,
                        resource_name,
                        client_ip, service_ip, service_port, op_content, op_result, err_code, err_msg])
client.insert(table, insert_data, column_names=column_list)
client.close()
